package com.ngc.mall.domain;

import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.core.type.TypeReference;
import com.ngc.mall.bo.Spec;
import com.ngc.mall.common.configure.mybatisPlus.BaseDomain;
import com.ngc.mall.util.GenericAndJson;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.math.BigDecimal;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

@TableName("sku")
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Data
public class SkuDomain extends BaseDomain {


    /**
     * 库存
     */
    private Integer stock;


    /**
     * 图片
     */
    private String img;


    private Long spuId;

    private Long categoryId;

    private String title;

    /**
     * 折扣价
     */
    private BigDecimal discount;

    /**
     * 原价
     */
    private BigDecimal price;

//    /**
//     * 成本价
//     */
//    private BigDecimal cost;

    /**
     * 是否上架
     */
    private Boolean isEnabled;

    @TableField(exist = false)
    private String specs;

    public BigDecimal getActualPrice(){
            return discount!=null?discount:price;
    }

    public List<Spec> getSpecs() {
        if (this.specs == null) {
            return Collections.emptyList();
        }
        return GenericAndJson.jsonToObject(this.specs, new TypeReference<List<Spec>>(){});
    }

    public void setSpecs(List<Spec> specs) {
        if(specs.isEmpty()){
            return;
        }
        this.specs = GenericAndJson.objectToJson(specs);
    }

    @JsonIgnore
    public List<String> getSpecValueList() {
        return this.getSpecs().stream().map(Spec::getValue).collect(Collectors.toList());
    }

}
